package pl.edu.project.view;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.Locale;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.freixas.jcalendar.DateEvent;
import org.freixas.jcalendar.DateListener;
import org.freixas.jcalendar.JCalendar;

@SuppressWarnings("serial")
public class AddEventWindow extends JDialog {
	private class ActionCancel implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	} 

	private class ActionOK implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			dispose();
			// dopisac
		}
	}

	private class ChangeAlarm implements ChangeListener {
		public void stateChanged(ChangeEvent e) {
			AbstractButton button = (AbstractButton) e.getSource();
			if (button.getModel().isSelected())
				isAlarm = true;
			else
				isAlarm = false;
		}
	}

	private class ChangeAlarmHour implements ChangeListener {
		public void stateChanged(ChangeEvent e) {
			JSpinner spinner = (JSpinner) e.getSource();
			alarmH = new Integer(spinner.getValue().toString());
		}
	}

	private class ChangeAlarmMinute implements ChangeListener {
		public void stateChanged(ChangeEvent e) {
			JSpinner spinner = (JSpinner) e.getSource();
			alarmM = new Integer(spinner.getValue().toString());
		}
	}

	private class markTextField implements FocusListener {
		public void focusGained(FocusEvent e) {
			JTextField f = (JTextField) e.getSource();
			f.selectAll();
		}

		@Override
		public void focusLost(FocusEvent arg0) {
			// TODO Auto-generated method stub

		}
	}

	private class MyDateListener implements DateListener {

		public void dateChanged(DateEvent e) {
			selectedTime = e.getSelectedDate();
		}
	}

	private JCheckBox alarm;
	private int alarmH;
	private JSpinner alarmHours;
	private JLabel alarmLabel;

	private int alarmM;
	private JSpinner alarmMinutes;
	private JLabel alarmTimeJLabel;
	private JButton cancelButton;

	private boolean editMode;

	private JTextField eventName;

	private JTextField eventPlace;

	private JCalendar eventTime;

	private JLabel hourLabel;

	private boolean isAlarm;

	private CalendarFrame mainWindow;

	/**
	 * 
	 */
	private JButton okButton;

	private Calendar selectedTime;

	public AddEventWindow(JFrame owner) {
		super(owner, "Dodaj Termin");
		initWindow(owner);

		editMode = false;

		setResizable(false);
		setVisible(true);
	}

	private void initWindow(JFrame owner) {
		setSize(330, 190);
		setLocation(owner.getLocation().x + 45, owner.getLocation().y + 115);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		// this.mainWindow = (CallendarFrame) owner;

		setData();
		selectedTime = eventTime.getCalendar();
		// layout
		Container contentPane = getContentPane();
		contentPane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets.set(1, 5, 1, 5);
		c.anchor = GridBagConstraints.LINE_START;
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 0;
		add(hourLabel, c);
		c.gridx = 1;
		add(eventTime, c);
		c.gridy = 1;
		c.gridx = 0;
		add(alarmLabel, c);
		c.gridx = 1;
		add(alarm, c);
		c.gridy = 2;
		c.gridx = 0;
		add(alarmTimeJLabel, c);
		JPanel hourMin = new JPanel();
		hourMin.add(alarmHours);
		hourMin.add(alarmMinutes);
		c.gridx = 1;
		add(hourMin, c);
		c.gridy = 3;
		c.gridx = 0;
		add(eventName, c);
		c.gridx = 1;
		add(eventPlace, c);
		c.insets.set(10, 5, 5, 5);
		c.gridy = 4;
		c.gridx = 1;
		JPanel okCancel = new JPanel();
		okCancel.add(okButton);
		okCancel.add(cancelButton);
		add(okCancel, c);
	}

	private void setData() {
		this.okButton = new JButton("OK");
		okButton.addActionListener(new ActionOK());
		okButton.registerKeyboardAction(new ActionOK(),
				KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
				JComponent.WHEN_IN_FOCUSED_WINDOW);
		this.cancelButton = new JButton("Anuluj");
		cancelButton.addActionListener(new ActionCancel());
		cancelButton.registerKeyboardAction(new ActionCancel(),
				KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
				JComponent.WHEN_IN_FOCUSED_WINDOW);
		this.hourLabel = new JLabel("Godzina zdarzenia");
		this.alarmLabel = new JLabel("Alarm");
		this.alarmTimeJLabel = new JLabel("Wyprzedzenie alarmu [H:M]");
		this.alarm = new JCheckBox();
		alarm.addChangeListener(new ChangeAlarm());
		this.alarmHours = new JSpinner(new SpinnerNumberModel(0, 0, 168, 1));
		alarmHours.addChangeListener(new ChangeAlarmHour());
		alarmHours.setValue(alarmHours.getValue());
		this.alarmMinutes = new JSpinner(new SpinnerNumberModel(0, 0, 59, 1));
		alarmMinutes.addChangeListener(new ChangeAlarmMinute());
		alarmMinutes.setValue(alarmMinutes.getValue());
		this.eventName = new JTextField("Nazwa zdarzenia");
		eventName.addFocusListener(new markTextField());
		this.eventPlace = new JTextField("Miejsce zdarzenia");
		eventPlace.addFocusListener(new markTextField());
		// wylapywacz daty
		MyDateListener listener = new MyDateListener();
		// pobranie godziny wydarzenia
		eventTime = new JCalendar(Calendar.getInstance(), Locale.getDefault(),
				JCalendar.DISPLAY_TIME, false, "HH:mm");
		eventTime.addDateListener(listener);
	}

	public JCheckBox getAlarm() {
		return alarm;
	}

	public void setAlarm(JCheckBox alarm) {
		this.alarm = alarm;
	}

	public int getAlarmH() {
		return alarmH;
	}

	public void setAlarmH(int alarmH) {
		this.alarmH = alarmH;
	}

	public JSpinner getAlarmHours() {
		return alarmHours;
	}

	public void setAlarmHours(JSpinner alarmHours) {
		this.alarmHours = alarmHours;
	}

	public JLabel getAlarmLabel() {
		return alarmLabel;
	}

	public void setAlarmLabel(JLabel alarmLabel) {
		this.alarmLabel = alarmLabel;
	}

	public int getAlarmM() {
		return alarmM;
	}

	public void setAlarmM(int alarmM) {
		this.alarmM = alarmM;
	}

	public JSpinner getAlarmMinutes() {
		return alarmMinutes;
	}

	public void setAlarmMinutes(JSpinner alarmMinutes) {
		this.alarmMinutes = alarmMinutes;
	}

	public JLabel getAlarmTimeJLabel() {
		return alarmTimeJLabel;
	}

	public void setAlarmTimeJLabel(JLabel alarmTimeJLabel) {
		this.alarmTimeJLabel = alarmTimeJLabel;
	}

	public JButton getCancelButton() {
		return cancelButton;
	}

	public void setCancelButton(JButton cancelButton) {
		this.cancelButton = cancelButton;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public JTextField getEventName() {
		return eventName;
	}

	public void setEventName(JTextField eventName) {
		this.eventName = eventName;
	}

	public JTextField getEventPlace() {
		return eventPlace;
	}

	public void setEventPlace(JTextField eventPlace) {
		this.eventPlace = eventPlace;
	}

	public JCalendar getEventTime() {
		return eventTime;
	}

	public void setEventTime(JCalendar eventTime) {
		this.eventTime = eventTime;
	}

	public JLabel getHourLabel() {
		return hourLabel;
	}

	public void setHourLabel(JLabel hourLabel) {
		this.hourLabel = hourLabel;
	}

	public boolean isAlarm() {
		return isAlarm;
	}

	public void setAlarm(boolean isAlarm) {
		this.isAlarm = isAlarm;
	}

	public CalendarFrame getMainWindow() {
		return mainWindow;
	}

	public void setMainWindow(CalendarFrame mainWindow) {
		this.mainWindow = mainWindow;
	}

	public JButton getOkButton() {
		return okButton;
	}

	public void setOkButton(JButton okButton) {
		this.okButton = okButton;
	}

	public Calendar getSelectedTime() {
		return selectedTime;
	}

	public void setSelectedTime(Calendar selectedTime) {
		this.selectedTime = selectedTime;
	}
	
	
}
